package com.example.springboot.service.impl.Security;

import com.example.springboot.config.ServiceException;
import com.example.springboot.dao.UserMapper;
import com.example.springboot.domain.LoginUser;
import com.example.springboot.domain.Role;
import com.example.springboot.domain.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    UserMapper userMapper;

    BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();

    @Override
    public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
        User user=new User();
        user.setPhone(name);
        User u=userMapper.login(user);
//        System.out.println(bcryptPasswordEncoder.matches("06a261947610a165b1587ab6c30a5746",u.getPassword()));
        if(Objects.isNull(u)){
            throw new ServiceException("404","错误");
        }
        List<Role> allRoles = userMapper.findUserRelationRoleById(u.getId());
        System.out.println(allRoles);
        return new LoginUser(u,allRoles);
    }
}
